package com.oig.sys.base.controller;


import com.oig.common.module.SysResponse;
import com.oig.common.validator.GroupAdd;
import com.oig.common.validator.GroupEdit;
import com.oig.sys.base.dto.UnitConvRateDto;
import com.oig.sys.base.service.ITbUnitConvRateService;
import com.oig.sys.base.vo.UnitConvRateVo;
import com.oig.sys.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "单位换算")
@RestController
@RequestMapping("/unitCR")
@Slf4j
public class TbUnitConvRateController {

    private final ITbUnitConvRateService unitConvRateService;

    @Autowired
    public TbUnitConvRateController(ITbUnitConvRateService unitConvRateService){
        this.unitConvRateService = unitConvRateService;
    }


    @PreAuthorize("@pms.hasPermission('/unitCR/add')")
    @SysLog(value="添加单位换算系数",apiUrl="/unitCR/add")
    @Operation(summary = "添加单位换算系数")
    @PostMapping(value = "/add")
    public UnitConvRateVo add(@RequestBody @Validated(GroupAdd.class) UnitConvRateDto unitConvRateDto){
        log.info("add unit convert:{}",unitConvRateDto);
        return this.unitConvRateService.addUnitConvRate(unitConvRateDto);
    }

    @PreAuthorize("@pms.hasPermission('/unitCR/update')")
    @SysLog(value="添加单位换算系数",apiUrl="/unitCR/update")
    @Operation(summary = "添加单位换算系数")
    @PostMapping(value = "/update")
    public UnitConvRateVo update(@RequestBody @Validated(GroupEdit.class) UnitConvRateDto unitConvRateDto){
        log.info("update unit convert:{}",unitConvRateDto);
        return this.unitConvRateService.updateUnitConvRate(unitConvRateDto);
    }


    @PreAuthorize("@pms.hasPermission('/unitCR/del')")
    @SysLog(value="逻辑删除单位换算系数",apiUrl="/unitCR/del")
    @Operation(summary = "逻辑删除单位换算系数")
    @PostMapping(value = "/del/{id}")
    public SysResponse<Integer> del(@PathVariable("id") Integer id){
        log.info("cancel unit convert code={}", id) ;
        this.unitConvRateService.delUnitConvRate(id);
        return SysResponse.ok(id,"逻辑删除单位转换配置成功");
    }


    @Operation(summary = "拿换算系数")
    @GetMapping(value = "/getUnitConvRate")
    public UnitConvRateVo getUnitConvRate(@RequestParam("unitCode")String unitCode, @RequestParam("targetUnitCode")String targetUnitCode){
        log.info("query unit conv rate:{},{}",unitCode,targetUnitCode);
        return this.unitConvRateService.getUnitConvRate(unitCode,targetUnitCode);
    }

}
